﻿using Loki.Client;
using System.Text.Json.Nodes;

namespace Test
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var url = "http://10.0.10.142:3100";
            var client = new LokiClient(url);

            //app、level （must required）
            
            var stream = new LokiStream();
            stream.AddLabel("app", "qms");
            stream.AddLabel("level", "warning"); //debug、info、warning、error、fatal、critical、unknown
            stream.AddLabel("host", "192.168.137.28");
            stream.AddLabel("port", "80");

            stream.AddValue(DateTimeOffset.UtcNow, "这是个测试==>" + stream.stream["level"]);
            stream.AddValue(DateTimeOffset.UtcNow, new { a = 1, b = 2, c = 102.88, name = "你好，这是什么==>" + stream.stream["level"] });

            //insert
            await client.InsertAsync(stream);


            var stream2 = new LokiStream();
            stream2.AddLabel("app", "qms");
            stream2.AddLabel("level", "error");
            stream2.AddLabel("host", "192.168.137.30");
            stream2.AddLabel("port", "6379");

            stream2.AddValue(DateTimeOffset.UtcNow, "这是个测试stream2==>" + stream2.stream["level"]);
            stream2.AddValue(DateTimeOffset.UtcNow, new { a = 1, b = 2, c = 3000, name = "你好，测试stream2==>" + stream2.stream["level"] });

            try
            {
                throw new AccessViolationException("Access denied");
            }
            catch (Exception ex)
            {
                stream2.AddValue(DateTimeOffset.UtcNow, ex);
            }

            //insert list
            await client.InsertAsync([stream, stream2]);


            //query
            var query = new LokiQueryRange
            {
                query = """
                {app="qms"}
                """,
                start = DateTimeOffset.UtcNow.AddDays(-2).ToUnixNanosecondsString(),
                end = DateTimeOffset.UtcNow.ToUnixNanosecondsString(),
                direction = "backward",
                limit = 100,
                step = "5m"
            };
            var queryData = await client.QueryAsync<JsonObject>(query);

            Console.WriteLine("Hello, World!");
        }
    }
}
